package cn.liyiming.bbs.modular.system.controller;

import cn.liyiming.bbs.util.CommonUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * @program: bbsblog
 * @description: 文件上传接口
 * @author: 李一鸣
 * @create: 2020-04-10 21:43
 **/
@Controller
public class FileUploadController extends BaseController {

    /**
     * 上传文件本地路径
     */
    @Value("${upload.path}")
    private String path;
    /**
     * 上传文件相对路径
     */
    @Value("${upload.relativePath}")
    private String relativePath;

    @RequestMapping("/rest/doUpload")
    @ResponseBody
    public Object fileUpload(@RequestParam("edit") MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String src = CommonUtil.uploadFile(file,path,relativePath,originalFilename);
        HashMap data = new HashMap(16);
        data.put("id",relativePath+src);
        return new SuccessResponseData(0,"上传文件成功",data);
    }

}
